package com.imageim.yimirror.common.ui.btn;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.imageim.yimirror.R;

/**
 * Created by Yuaihen.
 * on 2018/9/19
 * 矩形点击放大  button
 */
public class BtnRectangle extends SurfaceView {

    private Paint mPaint;
    //边框宽度
    private static float strokeWidth = 2;
    private SurfaceHolder mHolder;
    private boolean isDraw;
    //背景颜色
    private int backgroundColor;
    private static final int STATE_UNSELECTED = 0;
    private static final int STATE_SELECTED = 1;
    private int state;
    private int mWidthSize, mHeightSize, mRx, mRy;
    private float mSmallRect;
    private float mGap;
    private boolean touchable = true;
    private Thread mThread;

    public BtnRectangle(Context context) {
        super(context);
        init(context, null);
    }

    public BtnRectangle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public BtnRectangle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BtnRectView);
        backgroundColor = typedArray.getColor(R.styleable.BtnRectView_background_color, 0xffff0000);
        strokeWidth = typedArray.getFloat(R.styleable.BtnRectView_stroke_width, 2);
        state = typedArray.getInt(R.styleable.BtnRectView_select_state, 0);
        typedArray.recycle();

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
        mPaint.setStrokeWidth(strokeWidth);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (!touchable) {
                            return true;
                        }

                        if (selectListener != null) {
                            selectListener.onSelect(BtnRectangle.this, getState());
                        }
                        break;
                    default:
                }
                return true;
            }
        });

        mHolder = getHolder();
        mHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (!isDraw) {
                    isDraw = true;
                    mThread = new Thread(new MyRunnale());
                    mThread.start();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                isDraw = false;
            }
        });

    }


    public int getBackgroundColor() {
        return backgroundColor;
    }

    @Override
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    private OnSelectListener selectListener;

    public interface OnSelectListener {
        void onSelect(BtnRectangle btn, boolean isSelected);
    }

    public void setSelectListener(OnSelectListener selectListener) {
        this.selectListener = selectListener;
    }

    public void setTouchable(boolean touchable) {
        this.touchable = touchable;
    }

    public boolean getState() {
        return state != STATE_UNSELECTED;
    }

    private void setState(int state) {
        if (this.state != state) {
            this.state = state;
        }

        isDraw = true;
        if (mThread != null) {
            mThread.start();
        }
        //        ThreadPoolManager.getInstance().getThreadPool().execute(new MyRunnale());
    }

    public void setBtnSelected(boolean selected) {
        if (touchable) {
            if (selected) {
                setState(STATE_SELECTED);
            } else {
                setState(STATE_UNSELECTED);
            }
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        mWidthSize = getMeasuredWidth();
        mHeightSize = getMeasuredHeight();
        mRx = mWidthSize / 2;
        mRy = mHeightSize / 2;

        //内圈的宽高
        mSmallRect = mWidthSize * 0.35f;
        //内圈与外圈的间隙
        mGap = (mHeightSize - mSmallRect) / 2;
    }


    private class MyRunnale implements Runnable {

        @Override
        public void run() {
            while (isDraw) {
                drawView();
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private float speed = 0f;
    private float left, top, right, bottom;

    private void drawView() {
        Canvas canvas = mHolder.lockCanvas();
        if (canvas == null) {
            return;
        }
        //背景透明
        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

        if (state == STATE_SELECTED) {
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.WHITE);
            mPaint.setAntiAlias(true);
            canvas.drawRect(0, 0, mWidthSize, mHeightSize, mPaint);

            //内圈变大填充满
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setColor(backgroundColor);
            canvas.drawRect(left, top, right, bottom, mPaint);
            if (right >= mWidthSize) {
                isDraw = false;
                speed = 0;
                left = 0;
                top = 0;
                right = 0;
                bottom = 0;
            } else {
                speed += 1;
                left -= speed;
                top -= speed;
                right += speed;
                bottom += speed;
            }
        } else {
            //未选中  画边框
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.WHITE);
            mPaint.setAntiAlias(true);
            canvas.drawRect(0, 0, mWidthSize, mHeightSize, mPaint);

            //再画内圈的正方形块
            mPaint.setColor(backgroundColor);
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawRect(mGap, mGap, mWidthSize - mGap, mWidthSize - mGap, mPaint);
            left = mGap;
            top = mGap;
            right = mWidthSize - mGap;
            bottom = mWidthSize - mGap;
            isDraw = false;
        }

        if (mHolder != null) {
            try {
                mHolder.unlockCanvasAndPost(canvas);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        isDraw = false;
    }
}
